import os
from email.parser import Parser
from collections import Counter
rootdir = "/home/shzhuoyu/downloads/maildir"

def email_analyse(inputfile,from_to_list,from_cc_list,from_bcc_list):
    with open(inputfile, "r", encoding='utf-8') as f:
        try:
            data = f.read()
        except:
            with open(inputfile, "r", encoding='ISO-8859-1') as f:
                data = f.read()
    email = Parser().parsestr(data)
    if email['to']:
        email_to = email['to']
        email_to = email_to.replace("\n", "")
        email_to = email_to.replace("\t", "")
        email_to = email_to.replace(" ", "")
        email_to = email_to.split(",")
        for email_to_1 in email_to:
            from_to_list.append(email['from']+' -> '+email_to_1)

    if email['cc']:
        email_cc = email['cc']
        email_cc = email_cc.replace("\n", "")
        email_cc = email_cc.replace("\t", "")
        email_cc = email_cc.replace(" ", "")
        email_cc = email_cc.split(",")
        for email_to_2 in email_cc:
            from_cc_list.append(email['from']+' -> '+email_to_2)

    if email['bcc']:
        email_bcc = email['bcc']
        email_bcc = email_bcc.replace("\n", "")
        email_bcc = email_bcc.replace("\t", "")
        email_bcc = email_bcc.replace(" ", "")
        email_bcc = email_bcc.split(",")
        for email_to_3 in email_bcc:
            from_bcc_list.append(email['from']+' -> '+email_to_3)
    # to_email_list.append(email['to'])
    # from_email_list.append(email['from'])
    # email_body.append(email.get_payload())

from_to_list = []
from_cc_list = []
from_bcc_list = []

for directory, subdirectory, filenames in  os.walk(rootdir):
    print('now in: '+directory)
    for filename in filenames:
        email_analyse(os.path.join(directory, filename),from_to_list,from_cc_list,from_bcc_list)

cto=Counter(from_to_list)
ccc=Counter(from_cc_list)
cbcc=Counter(from_bcc_list)


with open("from_to_list.txt", "w") as f:
    for item in cto:
        if item:
            f.write(item+' '+str(cto[item]))
            f.write("\n")

with open("from_cc_list.txt", "w") as f:
    for item in ccc:
        if item:
            f.write(item+' '+str(ccc[item]))
            f.write("\n")\

with open("from_bcc_list.txt", "w") as f:
    for item in cbcc:
        if item:
            f.write(item+' '+str(cbcc[item]))
            f.write("\n")